package com.joolun.web.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.mall.entity.Advertisement;
import com.joolun.mall.service.IAdvertisementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 广告设置Controller
 * 
 * @author zhulj
 * @date 2022-02-15
 */
@RestController
@RequestMapping("/system/advertisement")
@Api(value = "system/advertisement", tags = "广告管理")
public class AdvertisementController extends BaseController
{
    @Autowired
    private IAdvertisementService advertisementService;

    /**
     * 查询广告设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询广告设置列表")
    public AjaxResult list(Page page, Advertisement advertisement)
    {
        return  AjaxResult.success(advertisementService.page(page,
                new QueryWrapper<Advertisement>(advertisement)));
    }

    /**
     * 导出广告设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:export')")
    @Log(title = "广告设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出广告设置列表")
    public AjaxResult export(Advertisement advertisement)
    {
        List<Advertisement> list = advertisementService.list(new QueryWrapper<Advertisement>(advertisement));
        ExcelUtil<Advertisement> util = new ExcelUtil<Advertisement>(Advertisement.class);
        return util.exportExcel(list, "advertisement");
    }

    /**
     * 获取广告设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取广告设置详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(advertisementService.getById(id));
    }

    /**
     * 新增广告设置
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:add')")
    @Log(title = "广告设置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "修改广告设置")
    public AjaxResult add(@RequestBody Advertisement advertisement)
    {
        return AjaxResult.success(advertisementService.save(advertisement));
    }

    /**
     * 修改广告设置
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:edit')")
    @Log(title = "广告设置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改广告设置")
    public AjaxResult edit(@RequestBody Advertisement advertisement)
    {
        return AjaxResult.success(advertisementService.updateById(advertisement));
    }

    /**
     * 删除广告设置
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:remove')")
    @Log(title = "广告设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除广告")
    public AjaxResult remove(@PathVariable Integer id)
    {
        return AjaxResult.success(advertisementService.removeById(id));
    }

    /**
     * 删除广告设置
     */
    @PreAuthorize("@ss.hasPermi('system:advertisement:remove')")
    @Log(title = "广告设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/deleteBatch/{ids}")
    @ApiOperation(value = "通过id集合删除广告")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return AjaxResult.success(advertisementService.removeByIds(Arrays.asList(ids)));
    }
}
